package com.bbzn.device.client.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.dto.index.IndexBuildingDTO;
import com.bbzn.device.client.dto.index.ProductDevCountDTO;
import com.bbzn.device.client.dto.index.SkuHatDTO;
import com.bbzn.device.client.dto.index.WorkSiteDTO;
import com.bbzn.device.client.dto.product.ProductDTO;
import com.bbzn.device.client.dto.product.ProductExistDTO;
import com.bbzn.device.client.enums.ColorEnum;
import com.bbzn.device.client.enums.ProductEnum;
import com.bbzn.device.client.utils.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.bbzn.device.client.dataobject.Product;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.bbzn.device.client.dao.product.ProductMapper;
import com.bbzn.device.client.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Product record) {
        return productMapper.insert(record);
    }

    @Override
    public int insertOrUpdate(Product record) {
        return productMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(Product record) {
        return productMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int insertSelective(Product record) {
        return productMapper.insertSelective(record);
    }

    @Override
    public Product selectByPrimaryKey(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Product record) {
        return productMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Product record) {
        return productMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Product> selectByAll(Product product) {
        return productMapper.selectByAll(product);
    }

    @Override
    public int updateBatch(List<Product> list) {
        return productMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<Product> list) {
        return productMapper.batchInsert(list);
    }

    @Override
    public PageInfo<Product> findAllPageSelective(ProductDTO record) {
        PageInfo<Product> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(record.getPageNum());
        pageInfo.setPageSize(record.getPageSize());
        pageInfo.setResultList(productMapper.getProductList(record));
        int count = productMapper.getProductListCount(record);
        pageInfo.setTotalRecord(count);
        return pageInfo;
    }

    @Override
    public List<Product> findByAll(Product product) {
        return productMapper.findByAll(product);
    }

    @Override
    public List<Map<String, Object>> getIndexSelf(Long userId) {
        return productMapper.getIndexSelf(userId);
    }

    @Override
    public List<Map<String, Object>> getIndexAssignment(Long userId) {
        return productMapper.getIndexAssignment(userId);
    }

    @Override
    public IndexBuildingDTO getBuildIndex(Long userId) {
        // 设备数据
        List<WorkSiteDTO> workSiteList = getWorkSiteList(userId);
        // 帽子sku统计
        List<SkuHatDTO> skuList = getSkuList(userId);
        IndexBuildingDTO index = new IndexBuildingDTO();
        index.setDeviceList(workSiteList);
        index.setSkuList(skuList);
        return index;
    }

    @Override
    public List<WorkSiteDTO> getWorkSiteList(Long userId) {
        List<Map<String, Object>> self = productMapper.getIndexSelf(userId);
        List<Map<String, Object>> assignment = productMapper.getIndexAssignment(userId);
        List<Map<String, Object>> onOffline = productMapper.getIndexOnOffline(userId);
        List<Map<String, Object>> onOfflineAssignment = productMapper.getOnOfflineAssignment(userId);
        List<WorkSiteDTO> list = new ArrayList<>();
        // 安全帽
        Long totalHat = 0L;
        Long onlineHat = 0L;
        Long offerHat = 0L;
        Long activatedHat = 0L;
        // 安全绳
        Long totalRope = 0L;
        Long onlineRope = 0L;
        Long offerRope = 0L;
        Long activatedRope = 0L;
        // 检测手环
        Long totalBracelet = 0L;
        Long onlineBracelet = 0L;
        Long offerBracelet = 0L;
        Long activatedBracelet = 0L;
        // 手持终端
        Long totalTerminal = 0L;
        Long onlineTerminal = 0L;
        Long offerTerminal = 0L;
        Long activatedTerminal = 0L;

        if (CollectionUtils.isNotEmpty(self)) {
            for (Map<String, Object> map : self) {
                String productId = map.get("productId").toString();
                Long count = Long.valueOf(map.get("count").toString());
                if (ProductEnum.PRO_HAT.getCode().equals(productId)) {
                    totalHat += count;
                } else if (ProductEnum.PRO_ROPE.getCode().equals(productId)) {
                    totalRope += count;
                } else if (ProductEnum.PRO_BRACELET.getCode().equals(productId)) {
                    totalBracelet += count;
                } else if (ProductEnum.PRO_TERMINAL.getCode().equals(productId)) {
                    totalTerminal += count;
                }
            }
        }
        if (CollectionUtils.isNotEmpty(assignment)) {
            for (Map<String, Object> map : assignment) {
                  String productId = map.get("productId").toString();
                Long count = Long.valueOf(map.get("count").toString());
                if (ProductEnum.PRO_HAT.getCode().equals(productId)) {
                    totalHat += count;
                } else if (ProductEnum.PRO_ROPE.getCode().equals(productId)) {
                    totalRope += count;
                } else if (ProductEnum.PRO_BRACELET.getCode().equals(productId)) {
                    totalBracelet += count;
                } else if (ProductEnum.PRO_TERMINAL.getCode().equals(productId)) {
                    totalTerminal += count;
                }
            }
        }

        if (CollectionUtils.isNotEmpty(onOffline)) {
            for (Map<String, Object> map : onOffline) {
                String productId = map.get("productId").toString();
                Long count = Long.valueOf(map.get("count").toString());
                String state = map.get("state").toString();
                if (ProductEnum.PRO_HAT.getCode().equals(productId)) {
                    if (count.equals(0)) {
                        continue;
                    }
                    // 0 离线 1 在线 2 未激活
                    if (state.equals("0")) {
                        offerHat += count;
                    } else if (state.equals("1")) {
                        onlineHat += count;
                    } else if (state.equals("2")) {
                        activatedHat += count;
                    }
                } else if (ProductEnum.PRO_ROPE.getCode().equals(productId)) {
                    if (count.equals(0)) {
                        continue;
                    }
                    // 0 离线 1 在线 2 未激活
                    if (state.equals("0")) {
                        offerRope += count;
                    } else if (state.equals("1")) {
                        onlineRope += count;
                    } else if (state.equals("2")) {
                        activatedRope = count;
                    }
                } else if (ProductEnum.PRO_BRACELET.getCode().equals(productId)) {
                    if (count.equals(0)) {
                        continue;
                    }
                    // 0 离线 1 在线 2 未激活
                    if (state.equals("0")) {
                        offerBracelet += count;
                    } else if (state.equals("1")) {
                        onlineBracelet += count;
                    } else if (state.equals("2")) {
                        activatedBracelet += count;
                    }
                } else if (ProductEnum.PRO_TERMINAL.getCode().equals(productId)) {
                    if (count.equals(0)) {
                        continue;
                    }
                    // 0 离线 1 在线 2 未激活
                    if (state.equals("0")) {
                        offerTerminal += count;
                    } else if (state.equals("1")) {
                        onlineTerminal += count;
                    } else if (state.equals("2")) {
                        activatedTerminal += count;
                    }
                }
            }
        }

        if (CollectionUtils.isNotEmpty(onOfflineAssignment)) {
            for (Map<String, Object> map : onOfflineAssignment) {
                String productId = map.get("productId").toString();
                Long count = Long.valueOf(map.get("count").toString());
                String state = map.get("state").toString();
                if (ProductEnum.PRO_HAT.getCode().equals(productId)) {
                    if (count.equals(0)) {
                        continue;
                    }
                    // 0 离线 1 在线 2 未激活
                    if (state.equals("0")) {
                        offerHat += count;
                    } else if (state.equals("1")) {
                        onlineHat += count;
                    } else if (state.equals("2")) {
                        activatedHat += count;
                    }
                } else if (ProductEnum.PRO_ROPE.getCode().equals(productId)) {
                    if (count.equals(0)) {
                        continue;
                    }
                    // 0 离线 1 在线 2 未激活
                    if (state.equals("0")) {
                        offerRope += count;
                    } else if (state.equals("1")) {
                        onlineRope += count;
                    } else if (state.equals("2")) {
                        activatedRope = count;
                    }
                } else if (ProductEnum.PRO_BRACELET.getCode().equals(productId)) {
                    if (count.equals(0)) {
                        continue;
                    }
                    // 0 离线 1 在线 2 未激活
                    if (state.equals("0")) {
                        offerBracelet += count;
                    } else if (state.equals("1")) {
                        onlineBracelet += count;
                    } else if (state.equals("2")) {
                        activatedBracelet += count;
                    }
                } else if (ProductEnum.PRO_TERMINAL.getCode().equals(productId)) {
                    if (count.equals(0)) {
                        continue;
                    }
                    // 0 离线 1 在线 2 未激活
                    if (state.equals("0")) {
                        offerTerminal += count;
                    } else if (state.equals("1")) {
                        onlineTerminal += count;
                    } else if (state.equals("2")) {
                        activatedTerminal += count;
                    }
                }
            }
        }



        // 安全帽
        WorkSiteDTO workSiteDTO = new WorkSiteDTO();
        workSiteDTO.setProductId(ProductEnum.PRO_HAT.getCode());
        workSiteDTO.setProductName(ProductEnum.PRO_HAT.getVal());
        workSiteDTO.setTotal(totalHat);
        workSiteDTO.setOnline(onlineHat);
        workSiteDTO.setOffer(offerHat);
        workSiteDTO.setActivated(activatedHat);
        list.add(workSiteDTO);

        // 安全绳
        workSiteDTO = new WorkSiteDTO();
        workSiteDTO.setProductId(ProductEnum.PRO_ROPE.getCode());
        workSiteDTO.setProductName(ProductEnum.PRO_ROPE.getVal());
        workSiteDTO.setTotal(totalRope);
        workSiteDTO.setOnline(onlineRope);
        workSiteDTO.setOffer(offerRope);
        workSiteDTO.setActivated(activatedRope);
        list.add(workSiteDTO);

        // 检测手环
        workSiteDTO = new WorkSiteDTO();
        workSiteDTO.setProductId(ProductEnum.PRO_BRACELET.getCode());
        workSiteDTO.setProductName(ProductEnum.PRO_BRACELET.getVal());
        workSiteDTO.setTotal(totalBracelet);
        workSiteDTO.setOnline(onlineBracelet);
        workSiteDTO.setOffer(offerBracelet);
        workSiteDTO.setActivated(activatedBracelet);
        list.add(workSiteDTO);

        // 手持终端
        workSiteDTO = new WorkSiteDTO();
        workSiteDTO.setProductId(ProductEnum.PRO_TERMINAL.getCode());
        workSiteDTO.setProductName(ProductEnum.PRO_TERMINAL.getVal());
        workSiteDTO.setTotal(totalTerminal);
        workSiteDTO.setOnline(onlineTerminal);
        workSiteDTO.setOffer(offerTerminal);
        workSiteDTO.setActivated(activatedTerminal);
        list.add(workSiteDTO);
        return list;
    }

    @Override
    public List<SkuHatDTO> getSkuList(Long userId) {
        List<String> self = productMapper.getSelfHatSkus(userId);
        List<String> assignment = productMapper.getAssignmentHatSkus(userId);
        long white1 = self.stream().filter(s -> JSONObject.parseObject(s).get("name").equals(ColorEnum.WHITE.getColer())).count();
        long yellow1 = self.stream().filter(s -> JSONObject.parseObject(s).get("name").equals(ColorEnum.YELLOW.getColer())).count();
        long red1 = self.stream().filter(s -> JSONObject.parseObject(s).get("name").equals(ColorEnum.RED.getColer())).count();
        long blue1 = self.stream().filter(s -> JSONObject.parseObject(s).get("name").equals(ColorEnum.BLUE.getColer())).count();

        long white2 = assignment.stream().filter(s -> JSONObject.parseObject(s).get("name").equals(ColorEnum.WHITE.getColer())).count();
        long yellow2 = assignment.stream().filter(s -> JSONObject.parseObject(s).get("name").equals(ColorEnum.YELLOW.getColer())).count();
        long red2 = assignment.stream().filter(s -> JSONObject.parseObject(s).get("name").equals(ColorEnum.RED.getColer())).count();
        long blue2 = assignment.stream().filter(s -> JSONObject.parseObject(s).get("name").equals(ColorEnum.BLUE.getColer())).count();

        List<SkuHatDTO> list = new ArrayList<>();

        // 白色
        SkuHatDTO skuHat = new SkuHatDTO();
        skuHat.setColor(ColorEnum.WHITE.getColer());
        skuHat.setCount(white1 + white2);
        list.add(skuHat);

        // 黄色
        skuHat = new SkuHatDTO();
        skuHat.setColor(ColorEnum.YELLOW.getColer());
        skuHat.setCount(yellow1 + yellow2);
        list.add(skuHat);

        // 红色
        skuHat = new SkuHatDTO();
        skuHat.setColor(ColorEnum.RED.getColer());
        skuHat.setCount(red1 + red2);
        list.add(skuHat);

        // 蓝色
        skuHat = new SkuHatDTO();
        skuHat.setColor(ColorEnum.BLUE.getColer());
        skuHat.setCount(blue1 + blue2);
        list.add(skuHat);
        return list;
    }

    @Override
    public List<String> getSelfHatSkus(Long userId) {
        return productMapper.getSelfHatSkus(userId);
    }


    @Override
    public List<String> getAssignmentHatSkus(Long userId) {
        return productMapper.getAssignmentHatSkus(userId);
    }


    @Override
    public int updateBatchSelective(List<Product> list) {
        return productMapper.updateBatchSelective(list);
    }

    @Override
    public List<ProductDevCountDTO> getProductByDevList(List<Long> userIds) {
        return productMapper.getProductByDevList(userIds);
    }

    @Override
    public List<ProductExistDTO> findByProducts() {
        return productMapper.findByProducts();
    }
}











